package com.sea.finance.service.impl;


import com.codecrane.core.util.DateUtils;
import com.sea.finance.entity.Inventory;
import com.sea.finance.entity.Payment;
import com.sea.finance.entity.RefusedTax;
import com.sea.finance.mapper.InventoryMapper;
import com.sea.finance.mapper.PaymentMapper;
import com.sea.finance.mapper.RefusedTaxMapper;
import com.sea.finance.service.InventoryService;
import com.sea.finance.service.PaymentService;
import com.sea.finance.service.RefusedTaxService;
import com.sea.operate.entity.RelatedSku;
import com.sea.operate.service.RelatedSkuService;
import com.sea.physical.entity.CreateDespatchSku;
import com.sea.physical.service.CreateDespatchSkuService;
import com.sea.physical.service.CreatePlanService;
import com.sea.supply.entity.BillRefused;
import com.sea.supply.service.BillRefusedService;
import com.sea.supply.service.ProduceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @author 陈鸿飞
 */
@Service
public class RefusedTaxServiceImpl implements RefusedTaxService {

    @Autowired
    private RefusedTaxMapper refusedTaxMapper;
    @Autowired
    private ProduceService produceService;
    @Autowired
    private CreatePlanService createPlanService;
    @Autowired
    private BillRefusedService billRefusedService;

    @Override
    public List<RefusedTax> findByCnd(RefusedTax refusedTax) {
        return refusedTaxMapper.findByCnd(refusedTax);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int save(RefusedTax refusedTax) {
        BillRefused billRefused=new BillRefused();
        String refusedPlanNo = refusedTax.getRefusedPlanNo();
        String[] list = refusedPlanNo.split(",");
        billRefused.setRefusedChannel(refusedTax.getRefusedChannel());
        billRefused.setRefusedNo(refusedTax.getRefusedNo());
        for(String planNo:list){
            billRefused.setRefusedPlanNo(planNo);
            BillRefused bRefused = billRefusedService.findByRefusedPlanNo(planNo);
            if(null==bRefused) {
                billRefusedService.save(billRefused);
            }else {
                billRefused.setId(bRefused.getId());
                billRefusedService.usave(billRefused);
            }
        }
        return refusedTaxMapper.insert(refusedTax);
    }

    @Override
    public int usave(RefusedTax refusedTax) {
        //如果是已退税 根据柜号更新状态
        if(null!=refusedTax.getRefusedStatus() && refusedTax.getRefusedStatus().equals(1)){
            String refusedPlanNo = refusedTax.getRefusedPlanNo();
            String[] planNo = refusedPlanNo.split(",");
            for(String str:planNo){
                createPlanService.updateRefused(str);
            }

        }
        return refusedTaxMapper.update(refusedTax);
    }

    @Override
    public RefusedTax findById(Integer id) {
        return refusedTaxMapper.findById(id);
    }

    @Override
    public int delete(Integer id) {
        return refusedTaxMapper.delete(id);
    }

    @Override
    public Integer updateRefusedStatus(String refusedPlanNo) {
        return refusedTaxMapper.updateRefusedStatus(refusedPlanNo);
    }
}
